package com.gaswell.canal.chain;

import com.gaswell.canal.Canalinterface;
import com.gaswell.pojo.Diagnosis;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用来报警用的，就是将Dignois的记录统计一下，存入Record中
 * @ClassName PoliceHandler
 * @Author xiaow
 * @DATE 2024/6/6 16:08
 **/
public class PoliceHandler extends Handler {
    public PoliceHandler() {
        this.successor=null;
    }

    @Autowired
    BeanFactory beanFactory;

    @Override
    public void handleRequest(Object object) {
        if (Diagnosis.class.isInstance(object)) {
            Diagnosis diagnosis = (Diagnosis) object;
            if (diagnosis.getCategory() != null) {
                try {
                    Canalinterface canalinterface = (Canalinterface) beanFactory.getBean(diagnosis.getCategory() + "CanalOperator");
                    canalinterface.operate(diagnosis);
                } catch (NoSuchBeanDefinitionException exception) {
                    exception.printStackTrace();
                }
            }
        }


        if (this.successor != null)
            this.successor.handleRequest(object);
    }
}
